package com.qk.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.qk.common.PageResult;
import com.qk.dto.UserDto;
import com.qk.entity.User;
import com.qk.mapper.UserMapper;
import com.qk.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public PageResult<User> list(UserDto userDto) {
        //1. 设置分页参数
        PageHelper.startPage(userDto.getPage(), userDto.getPageSize());

        //2. 调用mapper接口查询
        List<User> userList = userMapper.selectList(userDto);

        //3. 解析并封装结果
        Page<User> page = (Page<User>) userList;
        return new PageResult<>(page.getTotal(), page.getResult());
    }

    /**
     * 添加用户
     *
     * @param user
     */
    @Override
    public void add(User user) {
        // 1. 补全基础属性
        user.setCreateTime(LocalDateTime.now());
        user.setUpdateTime(LocalDateTime.now());

        // 2. 设置默认密码
        user.setPassword(user.getName() + "123");

        // 3. 调用mapper接口
        userMapper.insert(user);
    }
}
